package top.hkyzf.study.leetcode.array;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-1-19 10:53
 */
public class 二分查找_704 {
    public int search(int[] nums, int target) {
        int start = 0;
        int end = nums.length - 1;
        while (start <= end) {
            int middle = (end - start) / 2 + start;
            if(target > nums[middle]) {
                if(start == middle) {
                    start++;
                } else {
                    start = middle;
                }
            } else if(target < nums[middle]) {
                if(end == middle) {
                    end--;
                } else {
                    end = middle;
                }
            } else {
                return middle;
            }
        }
        return -1;
    }

    @Test
    public void testSearch () {
        int [] nums = {-1,0,3,5,9,12};
        int target = 2;
        System.out.println(search(nums, target));
    }
}
